package com.sunda.spmsweb.usercontroller;


import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsuser.service.ISpmsRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-01-25
 */
@RestController
@RequestMapping("/spmsRole")
@Api(tags = "系统角色信息表", description = "系统角色信息表接口")
public class SpmsRoleController {

    @Autowired
    ISpmsRoleService iSpmsRoleService;

    @RequestMapping("/getRoleList")
    @ApiOperation(value = "获取系统全部角色信息", notes = "获取系统全部角色详细信息", httpMethod = "GET")
    public ResponseResult getRoleList(){
        try {
            return ResponseResult.success().add("roleList", iSpmsRoleService.getRoleList());
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/createRole")
    @ApiOperation(value = "新增/更新角色信息", notes = "新增/更新角色信息。\n" +
            "uuid 为空则新增，新增数据时 roleId 也要有唯一性；uuid 不为空则更新角色信息；\n" +
            "{\n" +
            "\t\t\t\"roleCnName\": \"需求提报员\",\n" +
            "\t\t\t\"roleInfo\": \"需求提报员\",\n" +
            "\t\t\t\"roleId\": \"1001\",\n" +
            "\t\t\t\"updateUser\": \"999947\",\n" +
            "\t\t\t\"createUser\": \"999947\",\n" +
            "\t\t\t\"roleEnName\": \"Demand Requester\",\n" +
            "\t\t\t\"uuid\": \"161556d97ae44139ad21659c5fb98e00\"\n" +
            "\t\t}", httpMethod = "POST")
    public ResponseResult createRole(@RequestBody JSONObject jsonObject){
        try {
            return iSpmsRoleService.createRole(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/createRolePerms")
    @ApiOperation(value = "新增/更新角色权限关系信息", notes = "新增/更新角色权限关系信息。\n" +
            "roleUuid 必输，先删除 roleUuid 下所有权限编码；新建 roleUuid - permissionId 关系；\n" +
            "{\n" +
            "\t\"rolePermList\": [{\n" +
            "\t\t\"roleUuid\": \"38a089dbe2c44545ab1f101cedeec0dc\",\n" +
            "\t\t\"permissionId\": [\"10001\", \"10002\", \"10003\", \"10004\", \"10005\"]\n" +
            "\t}, {\n" +
            "\t\t\"roleUuid\": \"ff60bd7e73294417ab68bda207e2b5b0\",\n" +
            "\t\t\"permissionId\": [\"10001\", \"10002\", \"10003\", \"10004\"]\n" +
            "\t}]\n" +
            "}", httpMethod = "POST")
    public ResponseResult createRolePerms(@RequestBody JSONObject jsonObject){
        try {
            return iSpmsRoleService.createRolePerms(jsonObject);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

}
